<?php  
class ControllerModuleRichsnippetsProduct extends Controller {  
	private $error = array();
   
  	public function index() {
    	$this->load->language('module/richsnippetsproduct');
        $this->load->model('richsnippets/richsnippetsproduct');
        
        $result[]= $this->model_richsnippets_richsnippetsproduct->GetProductDetails();
         
        if(!empty($result[0])){
			foreach ($result as $results) {
				
			    $action = array();
			    $action[] = array(
					'text' => $this->language->get('text_edit'),
					'href' => $this->url->link('module/richsnippetsproduct/update', 'token=' . $this->session->data['token'] . '&edit_id=' . $results['id'], 'SSL')
			    );
			
			    $this->data['detail'][] = array(
			        'id'          => $results['id'],
					'business_function'  => $results['business_function'], 
					'date_added'  => date($this->language->get('british_date_format_short'), strtotime($results['date_added'])),
					'status'      => ($results['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')), 
					'selected'    => isset($this->request->post['selected']) && in_array($result['edit_id'], $this->request->post['selected']),
					'action'      => $action
				); 
			} 
        }
		
    	if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];

			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$this->data['breadcrumbs'] = array();

		$this->data['breadcrumbs'][] = array(
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
			'text'      => $this->language->get('text_home'),
			'separator' => FALSE
		);

		$this->data['breadcrumbs'][] = array(
			'href'      => $this->url->link('module/richsnippetsproduct', 'token=' . $this->session->data['token'], 'SSL'),
			'text'      => $this->language->get('heading_title'),
			'separator' => ' :: '
		);
		
		 
    	$this->document->setTitle($this->language->get('heading_title'));
    	$this->data['heading_title'] = $this->language->get('heading_title');
		$this->data['text_no_results'] = $this->language->get('text_no_results');
		$this->data['column_title'] = $this->language->get('column_title');
		$this->data['column_date_added'] = $this->language->get('column_date_added');
		$this->data['column_status'] = $this->language->get('column_status');
		$this->data['column_action'] = $this->language->get('column_action'); 
		$this->data['button_insert'] = $this->language->get('button_insert');	
		$this->data['button_publish'] = $this->language->get('button_publish');
		$this->data['button_delete'] = $this->language->get('button_delete');
		$this->data['product_gr_richsnippet'] = $this->language->get('product_gr_richsnippet');
		 
		
	    $this->data['insert'] = $this->url->link('module/richsnippetsproduct/insert', 'token=' . $this->session->data['token'], 'SSL');
	    $this->data['delete'] = $this->url->link('module/richsnippetsproduct/delete', 'token=' . $this->session->data['token'], 'SSL'); 
		$this->data['action'] = $this->url->link('module/richsnippetsproduct', 'token=' . $this->session->data['token'] , 'SSL');
	 
    	$this->template = 'module/richsnippetsproduct.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
    	
  	}
  	
  	
     public function insert() {    
		$this->load->language('module/richsnippetsproduct');
		$this->load->model('richsnippets/richsnippetsproduct');
		$this->document->setTitle($this->language->get('heading_title'));

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validateForm())) {  
			$this->model_richsnippets_richsnippetsproduct->addProductDetails($this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('module/richsnippetsproduct', 'token=' . $this->session->data['token'], 'SSL'));
		}

		$this->getForm();
	}

	
	public function update() {
		$this->load->language('module/richsnippetsproduct');
		$this->load->model('richsnippets/richsnippetsproduct');

		$this->document->setTitle($this->language->get('heading_title'));

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validateForm())) { 
			$this->model_richsnippets_richsnippetsproduct->editProductDetails($this->request->get['edit_id'], $this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('module/richsnippetsproduct', 'token=' . $this->session->data['token'], 'SSL'));
		}

		$this->getForm();
	}
	
	
	public function delete() {
		$this->load->language('module/richsnippetsproduct');
		$this->load->model('richsnippets/richsnippetsproduct');
		$this->document->setTitle($this->language->get('heading_title'));
 
		if (isset($this->request->post['selected']) /*&& $this->validateDelete()*/) {
			foreach ($this->request->post['selected'] as $edit_id) {
				$this->model_richsnippets_richsnippetsproduct->deleteProductDetails($edit_id);
			}

			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('module/richsnippetsproduct', 'token=' . $this->session->data['token'], 'SSL'));
		}

		$this->index();
	}
	
	
	private function getForm() {
		$this->load->language('module/richsnippetsproduct');

		$this->data['text_product'] = $this->language->get('text_product'); 
		$this->data['text_enabled'] = $this->language->get('text_enabled');
		$this->data['text_disabled'] = $this->language->get('text_disabled'); 
		$this->data['entry_business_function'] = $this->language->get('entry_business_function');
		$this->data['entry_payment_methods'] = $this->language->get('entry_payment_methods');
		$this->data['entry_delivery_methods'] = $this->language->get('entry_delivery_methods');
		$this->data['link_delivery_methods'] = $this->language->get('link_delivery_methods'); 
		$this->data['link_business_function'] = $this->language->get('link_business_function'); 
		$this->data['button_publish'] = $this->language->get('button_publish');
		$this->data['button_preview'] = $this->language->get('button_preview'); 
		$this->data['button_cancel'] = $this->language->get('button_cancel'); 
		$this->data['text_select_all'] = $this->language->get('text_select_all');
		$this->data['entry_status'] = $this->language->get('entry_status'); 
		$this->data['text_note'] = $this->language->get('text_note');
		$this->data['text_status'] = $this->language->get('text_status');
		$this->data['product_gr_richsnippet'] = $this->language->get('product_gr_richsnippet');
		$this->data['token'] = $this->session->data['token'];

		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

		if (isset($this->error['business_function'])) {
			$this->data['error_business_function'] = $this->error['business_function'];
		} else {
			$this->data['error_business_function'] = '';
		}
 
		$this->data['breadcrumbs'] = array();

		$this->data['breadcrumbs'][] = array(
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
			'text'      => $this->language->get('text_home'),
			'separator' => FALSE
		);

		$this->data['breadcrumbs'][] = array(
			'href'      => $this->url->link('module/richsnippetsproduct', 'token=' . $this->session->data['token'], 'SSL'),
			'text'      => $this->language->get('heading_title'),
			'separator' => ' :: '
		);

		if (!isset($this->request->get['edit_id'])) {
			$this->data['action'] = $this->url->link('module/richsnippetsproduct/insert', 'token=' . $this->session->data['token'], 'SSL');
		} else {
			$this->data['action'] = $this->url->link('module/richsnippetsproduct/update', 'token=' . $this->session->data['token'] . '&edit_id=' . $this->request->get['edit_id'], 'SSL');
		    $result= $this->model_richsnippets_richsnippetsproduct->GetProductDetailsbyId($this->request->get['edit_id']); 
		}
		
	     if (isset($this->request->post['business_function'])) {
			$this->data['business_function'] = $this->request->post['business_function'];
		} elseif (isset($result)) {
			$this->data['business_function'] = $result['business_function'];
		} else {
			$this->data['business_function'] = '';
		}
		
	 if (isset($this->request->post['payment_methods'])) {
			$this->data['payment_methods'] = $this->request->post['payment_methods'];
		} elseif (isset($result)) {
			$this->data['payment_methods'] = $result['payment_methods'];
		} else {
			$this->data['payment_methods'] = '';
		}
		
	 if (isset($this->request->post['delivery_methods'])) {
			$this->data['delivery_methods'] = $this->request->post['delivery_methods'];
		} elseif (isset($result)) {
			$this->data['delivery_methods'] = $result['delivery_methods'];
		} else {
			$this->data['delivery_methods'] = '';
		}
		 
	    if (isset($this->request->post['status'])) {
			$this->data['status'] = $this->request->post['status'];
		} elseif (isset($result)) {
			$this->data['status'] = $result['status'];
		} else {
			$this->data['status'] = '';
		}
		 
		$this->data['cancel'] = $this->url->link('module/richsnippetsproduct', 'token=' . $this->session->data['token'], 'SSL'); 
		$this->load->model('localisation/language'); 
		$this->data['languages'] = $this->model_localisation_language->getLanguages();
 
		$this->template = 'module/richsnippets/richsnippets_product_add.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);

		$this->response->setOutput($this->render());
	}
	
	private function validateForm() {
		if (!$this->user->hasPermission('modify', 'module/richsnippetsproduct')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		if (!$this->request->post['business_function']) {
			$this->error['business_function'] = $this->language->get('error_business_function');
		}
 

		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}
	}
}
?>